package com.subang.function.controller;

import java.util.*;
import javax.servlet.http.HttpServletResponse;

import com.subang.common.core.domain.entity.SysUser;
import com.subang.common.utils.SecurityUtils;
import com.subang.framework.web.service.UserIntegralService;
import com.subang.function.domain.SbUserStudyGroup;
import com.subang.function.service.ISbUserStudyGroupService;
import com.subang.system.service.ISysUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.subang.common.annotation.Log;
import com.subang.common.core.controller.BaseController;
import com.subang.common.core.domain.AjaxResult;
import com.subang.common.enums.BusinessType;
import com.subang.function.domain.SbStudyMark;
import com.subang.function.service.ISbStudyMarkService;
import com.subang.common.utils.poi.ExcelUtil;
import com.subang.common.core.page.TableDataInfo;

/**
 * 学习打卡Controller
 * 
 * @author subei
 * @date 2025-03-27
 */
@RestController
@RequestMapping("/function/mark")
public class SbStudyMarkController extends BaseController
{
    @Autowired
    private ISbStudyMarkService sbStudyMarkService;

    @Autowired
    private ISbUserStudyGroupService sbUserStudyGroupService;

    @Autowired
    private ISysUserService userService;

    @Autowired
    private UserIntegralService userIntegralService;

    /**
     * 查询学习打卡列表
     */
    @GetMapping("/list")
    public AjaxResult list(SbStudyMark sbStudyMark)
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        sbStudyMark.setUserId(user.getUserId());
        List<SbStudyMark> list = sbStudyMarkService.selectSbStudyMarkList(sbStudyMark);
        return AjaxResult.success(list);
    }

    @GetMapping("/allList")
    public AjaxResult allList(SbStudyMark sbStudyMark)
    {
        List<SbStudyMark> list = sbStudyMarkService.selectSbStudyMarkList(sbStudyMark);
        return AjaxResult.success(list);
    }


    @GetMapping("/groupList")
    public AjaxResult groupList(SbStudyMark sbStudyMark)
    {
        SbUserStudyGroup sbUserStudyGroup = new SbUserStudyGroup();
        sbUserStudyGroup.setGroupId(sbStudyMark.getStudyGroup());
        List<SbUserStudyGroup> userGroupList = sbUserStudyGroupService.selectSbUserStudyGroupList(sbUserStudyGroup);

        List<SbStudyMark> allLists = new ArrayList<>();
        for (SbUserStudyGroup group : userGroupList) {
            SbStudyMark query = new SbStudyMark();
            query.setUserId(group.getUserId());
            List<SbStudyMark> userList = sbStudyMarkService.selectTodaySbStudyMarkList(query);
            allLists.addAll(userList);
        }
        allLists.sort(Comparator.comparing(SbStudyMark::getStudyMarkDate));

        return AjaxResult.success(allLists);
    }

    @GetMapping("/groupUnPunchList")
    public AjaxResult groupUnPunchList(SbStudyMark sbStudyMark)
    {

        List<SbStudyMark> allLists = sbStudyMarkService.selectTodayUnmarkedUsersByGroup(sbStudyMark.getStudyGroup());
        for (SbStudyMark sbStudyMark1 : allLists) {
            SysUser UnPunchUser = userService.selectUserById(sbStudyMark1.getUserId());
            sbStudyMark1.setNickName(UnPunchUser.getNickName());
        }
        return AjaxResult.success(allLists);
    }

    @GetMapping("/groupYesterdayUnPunchList")
    public AjaxResult groupYesterdayUnPunchList(SbStudyMark sbStudyMark)
    {

        List<SbStudyMark> allLists = sbStudyMarkService.selectYesterdayUnmarkedUsersByGroup(sbStudyMark.getStudyGroup());
        for (SbStudyMark sbStudyMark1 : allLists) {
            SysUser UnPunchUser = userService.selectUserById(sbStudyMark1.getUserId());
            sbStudyMark1.setNickName(UnPunchUser.getNickName());
        }
        return AjaxResult.success(allLists);
    }

    @GetMapping("/groupMonthUnPunchList")
    public AjaxResult groupMonthUnPunchList(SbStudyMark sbStudyMark)
    {

        List<SbStudyMark> allLists = sbStudyMarkService.groupMonthUnPunchList(sbStudyMark.getStudyGroup());
        for (SbStudyMark sbStudyMark1 : allLists) {
            SysUser UnPunchUser = userService.selectUserById(sbStudyMark1.getUserId());
            sbStudyMark1.setNickName(UnPunchUser.getNickName());
        }
        return AjaxResult.success(allLists);
    }

    @GetMapping("/sumMonthStudyDurationByGroup")
    public AjaxResult sumMonthStudyDurationByGroup(SbStudyMark sbStudyMark)
    {
        List<SbStudyMark> allLists = sbStudyMarkService.sumMonthStudyDurationByGroup(sbStudyMark.getStudyGroup());
        for (SbStudyMark sbStudyMark1 : allLists) {
            SysUser UnPunchUser = userService.selectUserById(sbStudyMark1.getUserId());
            sbStudyMark1.setNickName(UnPunchUser.getNickName());
        }
        return AjaxResult.success(allLists);
    }

    @GetMapping("/sumMonthQuestionCountByGroup")
    public AjaxResult sumMonthQuestionCountByGroup(SbStudyMark sbStudyMark)
    {
        List<SbStudyMark> allLists = sbStudyMarkService.sumMonthQuestionCountByGroup(sbStudyMark.getStudyGroup());
        for (SbStudyMark sbStudyMark1 : allLists) {
            SysUser UnPunchUser = userService.selectUserById(sbStudyMark1.getUserId());
            sbStudyMark1.setNickName(UnPunchUser.getNickName());
        }
        return AjaxResult.success(allLists);
    }

    /**
     * 导出学习打卡列表
     */
    @PreAuthorize("@ss.hasPermi('function:mark:export')")
    @Log(title = "学习打卡", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SbStudyMark sbStudyMark)
    {
        List<SbStudyMark> list = sbStudyMarkService.selectSbStudyMarkList(sbStudyMark);
        ExcelUtil<SbStudyMark> util = new ExcelUtil<SbStudyMark>(SbStudyMark.class);
        util.exportExcel(response, list, "学习打卡数据");
    }

    /**
     * 获取学习打卡详细信息
     */
    @GetMapping(value = "/{studyMarkId}")
    public AjaxResult getInfo(@PathVariable("studyMarkId") String studyMarkId)
    {
        return success(sbStudyMarkService.selectSbStudyMarkByStudyMarkId(studyMarkId));
    }


    /**
     * 新增学习打卡
     */
    @Log(title = "学习打卡", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SbStudyMark sbStudyMark)
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        sbStudyMark.setUserId(user.getUserId());
        sbStudyMark.setNickName(user.getNickName());
        sbStudyMark.setStudyMarkDate(new Date());
        if (toAjax(sbStudyMarkService.insertSbStudyMark(sbStudyMark)).isSuccess()) {
            // 调用积分服务更新用户积分
            return userIntegralService.updateUserIntegral("dailyMark");
        } else {
            return AjaxResult.error("新增学习打卡失败");
        }
    }

    /**
     * 修改学习打卡
     */

    @Log(title = "学习打卡", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SbStudyMark sbStudyMark)
    {
        return toAjax(sbStudyMarkService.updateSbStudyMark(sbStudyMark));
    }

    /**
     * 删除学习打卡
     */
    @Log(title = "学习打卡", businessType = BusinessType.DELETE)
	@DeleteMapping("/{studyMarkIds}")
    public AjaxResult remove(@PathVariable String[] studyMarkIds)
    {
        return toAjax(sbStudyMarkService.deleteSbStudyMarkByStudyMarkIds(studyMarkIds));
    }

    @GetMapping("/rankingInfo")
    public AjaxResult rankingInfo(@RequestParam String rankType,@RequestParam(required = false)  String identityTab, @RequestParam(required = false) String groupId) {
        Map<String, Object> params = new HashMap<>();
        params.put("rankType", rankType);
        params.put("identityTab", identityTab);
        if (groupId != null && !groupId.trim().isEmpty()) {
            params.put("groupId", groupId);
        }
        return success(sbStudyMarkService.rankingInfo(params));
    }
}
